package com.hsq.androidproject.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.lenovo.androidproject3.R;
import com.hsq.androidproject.entity.QXzp;
import com.hsq.androidproject.ui.VideoViewActivity;
import com.hsq.androidproject.utils.glide.GlideRoundTransform;

import java.util.List;

/**
 * Created by Assass on 2017/3/19.
 */

public class QXzpAdapter extends BaseAdapter {
    List<QXzp.WorksBean> mData;
    Context context;

    public QXzpAdapter(List<QXzp.WorksBean> mData, Context context) {
        this.mData = mData;
        this.context = context;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.layout_griddtview, parent, false);
            viewHolder.iv_image = (ImageView) convertView.findViewById(R.id.dt_image);
            viewHolder.tv_title = (TextView) convertView.findViewById(R.id.dt_title);
            viewHolder.ll_click = (LinearLayout) convertView.findViewById(R.id.ll_click);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final QXzp.WorksBean worksBean = mData.get(position);
        String thumb = worksBean.getThumb();
        Glide.with(context)
                .load(thumb)
                .transform(new GlideRoundTransform(context, 3))
                .into(viewHolder.iv_image);

        String title = worksBean.getTitle();
        viewHolder.tv_title.setText(title);

        viewHolder.ll_click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String path = worksBean.getSources().get(0).getPage();
                Intent intent = new Intent(context, VideoViewActivity.class);
                intent.putExtra("path", path);
                context.startActivity(intent);
            }
        });
        return convertView;
    }

    class ViewHolder {
        private TextView tv_title;
        private ImageView iv_image;
        private LinearLayout ll_click;
    }
}
